package com.mqb.quartz;

import com.alibaba.fastjson.JSONObject;
import com.mqb.session.Temp;
import com.mqb.session.UserSession;
import io.netty.channel.Channel;
import org.apache.commons.lang3.tuple.Triple;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

/**
 * 定时任务，处理存放的未发送/未转发的消息
 */
public class StockMsgProcessJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<Triple<String, Channel, JSONObject>> tempMsgs =
                Temp.getTempMsgs();

        // TODO: 2022/9/10 上线后又马上下线，导致消息发送失败了？？ 加上ack机制，保证消息发送成功
        tempMsgs.stream().filter(triple -> UserSession.getSessionMap().containsKey(triple.getLeft()))
                .forEach(triple -> triple.getMiddle().writeAndFlush(triple.getRight()));
    }
}
